package edu.whut.controller;

import edu.whut.entity.Comment;
import edu.whut.entity.CommonResult;
import edu.whut.service.CommentService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/comment")
@RestController
public class CommentController {

    @Resource
    CommentService commentService;

    @RequestMapping("/comment")
    public CommonResult<Comment> addComment(@RequestBody Comment comment){
        if(commentService.addComment(comment)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/withdraw")
    public CommonResult<Comment> withdrawComment(@RequestBody Comment comment){
        if(commentService.deleteComment(comment)){
            return new CommonResult<>(200,"success",null);
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/get")
    public CommonResult<List<Comment>> getCreationComment(@RequestParam("creationId") Long creationId){
        if(commentService.getCommentById(creationId)!=null){
            return new CommonResult<>(200,"success", commentService.getCommentById(creationId));
        }
        return new CommonResult<>(400,"error",null);
    }

    @RequestMapping("/getCommentNum")
    public CommonResult<Integer> getCommentNum(@RequestParam("creationId") Long creationId){
        if(commentService.getCommentNumber(creationId)!=null){
            return new CommonResult<>(200,"success", commentService.getCommentNumber(creationId));
        }
        return new CommonResult<>(400,"error",null);
    }
}
